package com.liyujie.dyeingmachine.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import com.blankj.utilcode.util.LogUtils;


public class USBReceiver extends BroadcastReceiver {
    /**
     *
     */
    private final String ACTION_USB_PERMISSION = "ACTION.USB_PERMISSION";

    /**
     * @param context The Context in which the receiver is running.
     * @param intent  The Intent being received.
     */

    @Override
    public void onReceive(Context context, Intent intent) {
        /**
         *  这里可以拿到插入的USB设备对象
         */
        UsbDevice mUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        /**
         * 条件
         */
        switch (intent.getAction()) {
            case UsbManager.ACTION_USB_DEVICE_ATTACHED: // 插入USB设备
                LogUtils.d("USB插入");
                break;
            case UsbManager.ACTION_USB_DEVICE_DETACHED: // 拔出USB设备
                LogUtils.d("USB拔出");
                break;
            case ACTION_USB_PERMISSION:
                LogUtils.d("申请授权");
                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    //user choose YES for your previously popup window asking for grant perssion
                    // for this usb device

                } else {
                    //user choose NO for your previously popup window asking for grant perssion
                    // for this usb device

                }
                break;
            default:
                break;
        }
    }


}
